﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class admin_setTeacherPwd : System.Web.UI.Page
{
    db_teacher teacher = new db_teacher();
    protected void Page_Load(object sender, EventArgs e)
    {
        Server.Execute("../inc/checkPurview.aspx");  // 此处用户权限验证
    }
    protected void btn_select_Click(object sender, EventArgs e)
    {
        if (tb_teacherId.Text == "")
        {
            Response.Write("<script language=javascript>alert('请输入教师编号！');</script>");
        }
        else
        {
            string teacherId = Tools.Clearstr(tb_teacherId.Text, 20);
            DataSet ds = teacher.selectTeacherONE(teacherId);
            if (ds.Tables[0].Rows.Count != 0)
            {
                lbl_name.Text = ds.Tables[0].Rows[0]["teacherName"].ToString();
                lbl_teacherId.Text = ds.Tables[0].Rows[0]["teacherId"].ToString();
            }
            else {
                lbl_teacherId.Text = "编号";
                lbl_name.Text = "姓名";
                Response.Write("<script language=javascript>alert('找不到该用户！请重新输入。');</script>");
            }
        }
    }
    protected void cvr_teacherId_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (lbl_teacherId.Text.Equals("编号"))
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }
    protected void btn_ok_Click(object sender, EventArgs e)
    {
        
        if (Session["adminName"] == null)
        {
            Response.Write("<script language=javascript>alert('没有权限！');</script>");
            return;
        }
        
        if (Page.IsValid) {
            string teacherId = Tools.Clearstr(lbl_teacherId.Text,20);
            string pwd = tb_pwd_2.Text.Trim();
            if (pwd.Length < 6 || pwd.Length > 16)
            {
                Response.Write("<script language=javascript>alert('密码长度不符合要求！');</script>");
            }
            else
            {
                if (teacher.setPwd(teacherId, Tools.mymd5(Tools.Clearstr(pwd, 20))))
                {
                    res();
                    Response.Write("<script language=javascript>alert('用户密码设置成功！');</script>");
                }
                else
                {
                    Response.Write("<script language=javascript>alert('失败！');</script>");
                }
            }
        }
    }
    protected void btn_res_Click(object sender, EventArgs e)
    {
        res();
    }
    private void res() {
        tb_pwd_1.Text = "";
        tb_pwd_2.Text = "";
        tb_teacherId.Text = "";
        lbl_teacherId.Text = "编号";
        lbl_name.Text = "姓名";
    }
}